package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.JournalBean;
import com.project.bean.PowerBean;
import com.project.bean.RoleBean;
import com.project.bean.UserBean;
import com.project.mapper.IJournalMapper;
import com.project.mapper.IPowerMapper;
import com.project.mapper.IRoleMapper;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserMapper userMapper;
    @Autowired
    private IJournalMapper journalMapper;
    @Autowired
    private IRoleMapper roleMapper;
    @Autowired
    private IPowerMapper powerMapper;
    @Override
    public UserBean land(String userName, String pwd) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_userName",userName);
        qw.eq("u_pwd",pwd);
        UserBean userBean = userMapper.selectOne(qw);
        if (userBean!=null){
            LocalTime time = LocalTime.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
            JournalBean journal = new JournalBean("用户"+userBean.getUserName()+"在"+time.format(formatter)+"登陆了系统");
            journalMapper.insert(journal);
        }
        return userBean;
    }

    @Override
    public IPage<UserBean> findByItem(Integer pageNo, String userName) {
      QueryWrapper<UserBean> qw = new QueryWrapper<>();
      if (userName!=null&&userName.length()!=0){
          qw.like("u_userName",userName);
      }
        return userMapper.selectPage(new Page(pageNo,5),qw);
    }

    @Override
    public boolean add(UserBean user, Integer[] roleId) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_userName",user.getUserName());
        if (userMapper.selectOne(qw)==null){
            userMapper.insert(user);
            userMapper.addRoleId(user.getId(),roleId);
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean del(Integer id, UserBean loginUser) {
        if (loginUser.getId()==userMapper.selectById(id).getId()){
            return false;
        }else {
            userMapper.delRoleId(id);
           userMapper.deleteById(id);
            return true;
        }
    }

    @Override
    public UserBean findById(Integer id) {
        UserBean user =  userMapper.selectById(id);
        user.setRoleList(roleMapper.findByUserId(id));
        user.setPowerList(powerMapper.findByAll(id));
        return user;
    }

    @Override
    public void updateRol(Integer userId, Integer[] roleId) {
           userMapper.delRoleId(userId);
           userMapper.addRoleId(userId,roleId);

    }

    @Override
    public void updatePwd(Integer userId, String pwd) {
           UserBean user = new UserBean();
           user.setId(userId);
           user.setPwd(pwd);
           userMapper.updateById(user);
    }
}
